<?php
//namespace app\index\controller;
//use app\index\Controller;
//use think\Db;
//use think\Config;
namespace app\index\controller;
use app\index\Controller;
use think\Db;
use think\Validate;
use think\Config;



/**
 * @title 消息通知（模块09）
 * @description 接口说明
 * @group 消息通知
 * @header name:model_id require:0 default:09 desc:模块id
 * @groupremark 已使用错误代码1090001 1090002 1090003 1090004 1090005
 */
class Notify extends Controller{

    /**
     * @title 消息通知
     * @description 接口说明
     * @url /index/notify/get_list
     *
     * @author 庆锋
     *
     * @param name:token type:string require:1 desc:token参数
     * @param name:user_id type:int require:1 desc:司机的用户id
     * @param name:page type:int  desc:分页页数
     * @param name:type type:int  desc:
     * @remark type:driver_check_result司导审核结果,system_ordinary_info系统普通消息,news_order新的订单,checkouted已经结算,exam_pass考试通过,upgrade升级,driver_rank_apply司机升级申请,driver_degrade_appeal司机降级申诉,dispatch_traveler_group旅客分组信息,reset_group重置订单状态
     */

    public function get_list(){

        $data = input();

        $vmsg = [
            'token.require' => '缺少token',
            'user_id.require'   => '请填写司机的用户id',
        ];
        $validate = new Validate([
            'token'  => 'require',
            'user_id'=>"require",
        ],$vmsg);

        if (!$validate->check($data)) {
            $msg['error_id']="1090001";
            $msg['message']=$validate->getError();
            return $this->error($msg);
        }

        $map['status'] = true;
        $map['isdelete'] = false;
        $map['user_id'] = $data['user_id'];
        $listRows = $this->request->param('numPerPage') ? : Config::get("paginate.list_rows");
        $list = Db::name('notify')->where($map)
            ->order('id desc')
            ->paginate($listRows, false, ['query' => $this->request->get(),"page"=>input("page")]);
        $items=$list->items();

        foreach ($items as $key=>$value){
            if($value['dispatch_traveler_group_id']){
                $result = Db::name('dispatch_traveler_group')
                    ->alias('dtg')
                    ->join('dispatch d', 'dtg.dispatch_id=d.id', 'left')
                    ->join('order o', 'o.id=d.oid', 'left')
                    ->where('dtg.id', $value['dispatch_traveler_group_id'])
//                    ->where('status',1)
                    ->where('dtg.isdelete','0')
                    ->field('o.title,dtg.*')
                    ->find();
                $order_nums=Db::name("dispatch_traveler")
                    ->where('group_id',$result['id'])
                    ->group('oid')
                    ->column('count(traveler_id) as num');
                if($order_nums){
                    $num_str=implode('+',$order_nums);
                }
                $result['num_str']=$num_str;
                $items[$key]['is_order']=true;
                $items[$key]['order']=$result;
            }

        }

        if($list){
            $msg['list']=$items;
            $msg['total']=$list->total();
            $msg['per_page']=$list->listRows();
            $msg['current_page']=$list->currentPage();
            $msg['message']="获取成功";
            return $this->success($msg);
        }else{
            $msg['error_id']="1090002";
            $msg['message']="获取失败";
            return $this->error($msg);
        }
    }
    /**
     * @title 标记消息已读取
     * @description 接口说明
     * @url /index/notify/is_read
     *
     * @author 庆锋
     *
     * @param name:token type:string require:1 desc:token参数
     * @param name:id type:int require:1 desc:消息id
     */
    public function is_read(){
        $data = input();
        $isread = Db::name('notify')->field('isread,read_time')->where('id',$data['id'])->find();
        if($isread && empty($isread['read_time'])){
            $insert = [
                'isread'=>1,
                'read_time'=>time()
            ];
//            Db::name('notify')->where('id',$data['id'])->setField('isread', 1);
           $up =  Db::name('notify')->where('id',$data['id'])->update($insert);
           logs('up20190129',$up);
            $msg['message']="标记成功";
            return $this->success($msg);
        }elseif (!$isread['isread']){
            $insert = [
                'isread'=>1,
                'read_time'=>time()
            ];
            $up =  Db::name('notify')->where('id',$data['id'])->update($insert);
            $msg['message']="标记成功";
            return $this->success($msg);
        }else{
            $msg['error_id']="1090003";
            $msg['message']="标记失败";
            return $this->error($msg);
        }

    }
    /**
     * @title 标记消息删除
     * @description 接口说明
     * @url /index/notify/
     *
     * @author 梁敏
     *
     * @param name:token type:string require:1 desc:token参数
     * @param name:id type:int require:1 desc:消息id
     */
    public function delete(){
        $data = input();
        $notify = Db::name('notify')->where('id',$data['id'])->find();
        if($notify){
            Db::name('notify')->where('id',$data['id'])->setField('isdelete', 1);
            $msg['message']="删除成功";
            return $this->success($msg);
        }else{
            $msg['error_id']="1090004";
            $msg['message']="不存在该消息";
            return $this->error($msg);
        }

    }
    /**
     * @title 用户消息（获取未完结的订单列表）
     * @description 接口说明
     * @author 梁敏
     * @url /index/notify/get_order
     * @param name:token type:string require:1  other: desc:token
     * @param name:driver_user_id type:string require:1  other: desc:司机用户id
     * @method ~
     * @return error_id: 1090005 数据为空
     * @return list:旅客分组信息@
     * @list title:旅行团名称 id:派单编号 dispatch_time:分配时间 proceed_time:进行中时间 settlement_apply_time:结算申请时间 settlement_time:结算完成时间 dispatch_id:调配单id num:人数 driver_confirm:司机是否确认 go_date:开始时间 end_date:结束时间  dispatch_group_status:当前分组状态 is_settlement:是否结算 settlement_mode:结算方式 settlement_money:结算佣金 settlement_time:结算时间 car_form:用车形式
     */
    public function get_order()
    {

        $data = input();
        $items = Db::name('dispatch_traveler_group')
            ->alias('dtg')
            ->join('dispatch d', 'dtg.dispatch_id=d.id', 'left')
            ->where('dispatch_group_status', 'in','sendorders,proceed,achieve')
            ->where('dtg.driver_user_id', $data['driver_user_id'])
            ->field('dtg.*')
            ->select();
        if(!$items){
            $msg['error_id'] = '1090005';
            $msg['message'] = '数据为空';
            return $this->error($msg);
        }
        foreach ($items as $key=>$value){
            //人数
            $num=Db::name("dispatch_traveler")
                ->where('group_id',$value['id'])
                ->count('traveler_id');
            $items[$key]['num']=$num;
            $order_nums=Db::name("dispatch_traveler")
                ->where('group_id',$value['id'])
                ->group('oid')
                ->column('count(traveler_id) as num');
            $num_str="";
            if($order_nums){
                $num_str=implode('+',$order_nums);
            }
            $items[$key]['num_str']=$num_str;
            //旅客
            $travelers=Db::name("dispatch_traveler")
                ->alias('dt')
                ->where('dt.group_id',$value['id'])
                ->join('tp_order_traveler ot','ot.id=dt.traveler_id','left')
                ->field("ot.*")
                ->select();
            $items[$key]['travelers']=$travelers;
        }
            $msg['list']=$items;
            $msg['message']="获取成功";
            return $this->success($msg);

    }
}
